﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using AutoFacUtils;
using CashFlow.Db.Entity;
using CashFlow.ShopManage.Common;
using CashFlow.ShopManage.Models.Appointment;
using CashFlow.ShopManage.Models.Appointment.Result;
using CashFlow.ShopManage.Service;
using CashFlow.ShopManage.Validator.Appointment;
using CashFlow.ShopManage.Filters;

namespace CashFlow.ShopManage.Controllers
{
    public class AppointmentController : Controller
    {
        private IAdminService GetAdminService() { return AutoFacHelper.Get<IAdminService>(); }
        private IAppointmentService GetAppointmentService() { return AutoFacHelper.Get<IAppointmentService>(); }

        // GET: Appointment
        public ActionResult Index()
        {
            return View();
        }

        [ValidatorFilter(typeof(QueryAppointmentInfoModelValidator))]
        public ActionResult List(QueryAppointmentInfoModel model)
        {
            var adminService = GetAdminService();
            var ui = adminService.GetLoginUserInfo();
            var appointmentService = GetAppointmentService();
            var ls = appointmentService.QueryByWhere(ui.ShopManagerUserId
                    , model.Date
                    , model.Name
                    , model.Type);
            var ls2 = ls.Select(x => Convert(x)).ToList();
            return MyCommonTool.CreateLayUITableResult(true, "", ls2, ls2.Count);
        }

        private QueryAppointmentInfoResultModel Convert(AppointmentUserInfoView2 view)
        {
            return new QueryAppointmentInfoResultModel
            {
                Name = view.UserInfo.Name,
                InvitationCode = view.UserInfo.InvitationCode,
                Phone = view.UserInfo.Phone,
                Time = view.AppointmentInfo.Time.ToString("yyyy-MM-dd HH:mm:ss"),
                Type = view.AppointmentInfo.Type
            };
        }
    }
}